{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Renamed data\\1.doc to data\\1.doc\n"
     ]
    },
    {
     "ename": "FileExistsError",
     "evalue": "[WinError 183] 当文件已存在时，无法创建该文件。: 'data\\\\1.docx' -> 'data\\\\2.docx'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mFileExistsError\u001b[0m                           Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[7], line 37\u001b[0m\n\u001b[0;32m     35\u001b[0m rename \u001b[38;5;241m=\u001b[39m ReName()\n\u001b[0;32m     36\u001b[0m rename\u001b[38;5;241m.\u001b[39msetDir(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mdata\u001b[39m\u001b[38;5;124m'\u001b[39m)\n\u001b[1;32m---> 37\u001b[0m rename\u001b[38;5;241m.\u001b[39mstart()\n",
      "Cell \u001b[1;32mIn[7], line 30\u001b[0m, in \u001b[0;36mReName.start\u001b[1;34m(self)\u001b[0m\n\u001b[0;32m     28\u001b[0m old_path \u001b[38;5;241m=\u001b[39m os\u001b[38;5;241m.\u001b[39mpath\u001b[38;5;241m.\u001b[39mjoin(\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mdirectory, file)\n\u001b[0;32m     29\u001b[0m new_path \u001b[38;5;241m=\u001b[39m os\u001b[38;5;241m.\u001b[39mpath\u001b[38;5;241m.\u001b[39mjoin(\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mdirectory, new_name)\n\u001b[1;32m---> 30\u001b[0m os\u001b[38;5;241m.\u001b[39mrename(old_path, new_path)\n\u001b[0;32m     31\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mRenamed \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mold_path\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m to \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mnew_path\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m'\u001b[39m)\n",
      "\u001b[1;31mFileExistsError\u001b[0m: [WinError 183] 当文件已存在时，无法创建该文件。: 'data\\\\1.docx' -> 'data\\\\2.docx'"
     ]
    }
   ],
   "source": [
    "import os\n",
    "import glob\n",
    "\n",
    "\n",
    "class ReName:\n",
    "    def __init__(self):\n",
    "        self.directory = None\n",
    "\n",
    "    def setDir(self, directory):\n",
    "        \"\"\"设置文件路径\"\"\"\n",
    "        if os.path.isdir(directory):\n",
    "            self.directory = directory\n",
    "        else:\n",
    "            raise ValueError(f\"Invalid directory: {directory}\")\n",
    "\n",
    "    def start(self):\n",
    "        \"\"\"开始重命名\"\"\"\n",
    "        if not self.directory:\n",
    "            raise ValueError(\"Directory is not set. Use setDir() to set the directory first.\")\n",
    "\n",
    "        # 获取所有文件的路径\n",
    "        files = [f for f in os.listdir(self.directory) if os.path.isfile(os.path.join(self.directory, f))]\n",
    "        \n",
    "        # 临时重命名所有文件，避免重命名过程中发生冲突\n",
    "        temp_names = []\n",
    "        for i, file in enumerate(files):\n",
    "            file_extension = os.path.splitext(file)[1]\n",
    "            temp_name = f\"temp_{i}{file_extension}\"\n",
    "            old_path = os.path.join(self.directory, file)\n",
    "            temp_path = os.path.join(self.directory, temp_name)\n",
    "            os.rename(old_path, temp_path)\n",
    "            temp_names.append(temp_path)\n",
    "\n",
    "        # 按序号重命名\n",
    "        for i, temp_path in enumerate(temp_names, start=1):\n",
    "            file_extension = os.path.splitext(temp_path)[1]\n",
    "            new_name = f\"{i}{file_extension}\"\n",
    "            new_path = os.path.join(self.directory, new_name)\n",
    "            os.rename(temp_path, new_path)\n",
    "            print(f'Renamed {temp_path} to {new_path}')\n",
    "\n",
    "\n",
    "# 使用示例\n",
    "rename = ReName()\n",
    "rename.setDir('data')\n",
    "rename.start()\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
